package ren.helloworld.utils;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;

import java.lang.reflect.Method;

/**
 * Created by dafan on 2017/4/26 0026.
 */

public class VirtualKeyUtils {

	/**
	 * 是否显示了虚拟三键
	 *
	 * @param activity
	 * @return
	 */
	public static boolean showVirturlKey(Activity activity) {
		return getVirtualKeyHeight(activity) != 0;
	}

	/**
	 * 获取虚拟三键的高度，单位px
	 *
	 * @param activity
	 * @return
	 */
	public static int getVirtualKeyHeight(Activity activity) {
		return getHasVirtualKeyHeight(activity) - getNoHasVirtualKeyHeight(activity);
	}

	/**
	 * 获取屏幕尺寸，但是不包括虚拟功能高度
	 *
	 * @return
	 */
	public static int getNoHasVirtualKeyHeight(Activity activity) {
		int height = activity.getWindowManager().getDefaultDisplay().getHeight();
		return height;
	}

	/**
	 * 通过反射，获取包含虚拟键的整体屏幕高度
	 *
	 * @return
	 */
	public static int getHasVirtualKeyHeight(Activity activity) {
		int height = 0;
		Display display = activity.getWindowManager().getDefaultDisplay();
		DisplayMetrics dm = new DisplayMetrics();
		@SuppressWarnings("rawtypes")
		Class c;
		try {
			c = Class.forName("android.view.Display");
			@SuppressWarnings("unchecked")
			Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
			method.invoke(display, dm);
			height = dm.heightPixels;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return height;
	}

}
